<?
/*
 * Copyright 2004-2005 Sigve Indregard.
 *
 * This file is part of Laivsys.
 *
 * Laivsys is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Laivsys is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Laivsys; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Id: egenskapkart.php 6 2005-07-11 23:14:47Z say $
 */
 
include("../db.php");
include("auth.php");

//get the users
$sql = "SELECT * FROM deltakere WHERE LaivID=$laivid ORDER BY Navn";
$users = mysql_query($sql);
$users_num=mysql_num_rows($users);

//and the characteristics
$sql = "SELECT * FROM karakteristikker ORDER BY Karakteristikk";
$chars=mysql_query($sql);
$chars_num=mysql_num_rows($chars);

//some constants
define("USER_HEIGHT", 12);
define("USERNAME_WIDTH", 200);
define("CHAR_WIDTH", 12);
define("CHARNAME_HEIGHT", 150);
define("TOP_SPACE", 30);
define("BOTTOM_SPACE", 15);
define("LEFT_SPACE", 10);
define("RIGHT_SPACE", 10);

define("FONT", "cour.ttf");
define("FONTSIZE", 10);

//make our image
$width = LEFT_SPACE + USERNAME_WIDTH + $chars_num * CHAR_WIDTH + RIGHT_SPACE;
$height= TOP_SPACE + CHARNAME_HEIGHT + $users_num * USER_HEIGHT + BOTTOM_SPACE;
$im = @imagecreatetruecolor($width, $height);

//allocate colors
$background = imagecolorallocate($im, 255, 255, 255);
$linecolor = imagecolorallocate($im, 200, 200, 200);
$foreground = imagecolorallocate($im, 0, 0, 0);
$onbox = imagecolorallocate($im, 0, 255, 0);
$offbox = imagecolorallocate($im, 255, 0, 0);

//fill background
imagefill($im, 0, 0, $background);

//header
imagettftext($im, 24, 0, 3, 27, $foregorund, FONT, "Karakteristikk-kart");

//grid
//horizontal lines
for ($i=0; $i < $users_num; $i++) {
	imageline($im, 	LEFT_SPACE, 
			TOP_SPACE + CHARNAME_HEIGHT + USER_HEIGHT*$i, 
			$width - RIGHT_SPACE, 
			TOP_SPACE + CHARNAME_HEIGHT + USER_HEIGHT*$i,
			$linecolor);
}

//vertical lines
for ($i=0; $i < $chars_num; $i++) {
	imageline($im,	LEFT_SPACE + USERNAME_WIDTH + $i*CHAR_WIDTH,
			TOP_SPACE,
			LEFT_SPACE + USERNAME_WIDTH + $i*CHAR_WIDTH,
			$height - BOTTOM_SPACE,
			$linecolor);
}

//characteristics
$i=0;
while ($char = mysql_fetch_assoc($chars)) {
	$posx = LEFT_SPACE + USERNAME_WIDTH + $i*CHAR_WIDTH  + FONTSIZE;
	$posy = TOP_SPACE + CHARNAME_HEIGHT - 2;
	
	imagettftext($im, FONTSIZE, 90, $posx, $posy, $foreground, FONT, $char['Karakteristikk']);
	$i++;

	$a_char[] = $char['ID'];
}

//users
$i=0;
while ($user = mysql_fetch_assoc($users)) {
	$posx = LEFT_SPACE;
	$posy = TOP_SPACE + CHARNAME_HEIGHT + $i * USER_HEIGHT + FONTSIZE;

	imagettftext($im, FONTSIZE, 0, $posx, $posy, $foreground, FONT, $user['Navn']);
	$i++;

	$a_user[] = $user['ID'];
}

//dots
$row=0;
foreach($a_user as $uid) {
	$col=0;
	foreach($a_char as $cid) {
		$posx = LEFT_SPACE + USERNAME_WIDTH + $col * CHAR_WIDTH + 2;
		$posy = TOP_SPACE + CHARNAME_HEIGHT + $row * USER_HEIGHT + 2;
	
		$sql = "SELECT * FROM karakteristikklinje WHERE DeltakerID=$uid AND KarakteristikkID=$cid";
		$res=mysql_query($sql) or die(mysql_error());

		if (mysql_num_rows($res)) {
			imagefilledrectangle($im, $posx, $posy, 
				$posx +  (CHAR_WIDTH - 2), $posy + (USER_HEIGHT - 4), 
				$onbox);
		} else {
			/*
			imagefilledrectangle($im, $posx, $posy, 
				$posx + (CHAR_WIDTH - 2), $posy + (USER_HEIGHT - 2), 
				$offbox);
			*/
		}

		$col++;
	}
	$row++;
}
			

header("Content-type: image/png");
header("Content-Disposition: inline");
imagepng($im);
imagedestroy($im);

?>
